package com.andy.wan.android.mvp.net;

import android.text.TextUtils;

import com.andy.wan.android.mvp.bean.LoginBean;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author: luofan
 * @CreateDate: 2020/12/17 15:10
 * @Description:
 */
public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        String json = SPUtils.getInstance().getString(LoginBean.class.getName());
        LogUtils.e( "xxxxxxxxxxxx" + json);
        if (TextUtils.isEmpty(json)) {
            return chain.proceed(chain.request());
        } else {
            LoginBean loginBean = GsonUtils.fromJson(json, LoginBean.class);
            Request request = chain.request().newBuilder()
                    .addHeader("Cookie", "loginUserName=" + loginBean.getUsername())
                    .addHeader("Cookie", "loginUserPassword=" + loginBean.getPassword())
                    .build();
            return chain.proceed(request);
        }
    }
}